package com.itheima.reggie.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomerException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish>implements  DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;

    @Transactional//事务的标识注解,把方法体里面的业务看成是一个整体
    public void saveWithFlavor(DishDto dishDto) {
        this.save(dishDto);
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.forEach(flavor -> flavor.setDishId(dishDto.getId()));
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public Page<DishDto> selectPageByName(Integer page, Integer pageSize, String name) {
        Page<DishDto> dishDtoPage = new Page<>();//目标
        //分页查询dish表
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name), Dish::getName, name);//模糊查询
        Page<Dish> dishPage = new Page<>(page, pageSize);
        page(dishPage, queryWrapper);
        dishDtoPage.setTotal(dishPage.getTotal());
        ArrayList<DishDto> records = new ArrayList<>();
        dishPage.getRecords().forEach(dish -> {
            DishDto dishDto = new DishDto();
            Category category = categoryService.getById(dish.getCategoryId());
            String categoryName = category.getName();
            dishDto.setCategoryName(categoryName);
            BeanUtils.copyProperties(dish, dishDto);
            records.add(dishDto);
        });
        dishDtoPage.setRecords(records);
        return dishDtoPage;
    }


    @Override
    public DishDto getByIdWithFlavor(Long id) {
        DishDto dishDto = new DishDto();
        //查询菜品的基本信息
        Dish dish = this.getById(id);
        //查询菜品的口味信息
        QueryWrapper<DishFlavor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dish_id", id);
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        //属性的拷贝
        BeanUtils.copyProperties(dish, dishDto);
        //手动设置口味数据
        dishDto.setFlavors(flavors);
        return dishDto;
    }

    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        this.updateById(dishDto);
        QueryWrapper<DishFlavor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dish_id", dishDto.getId());
        dishFlavorService.remove(queryWrapper);
        dishDto.getFlavors().forEach(flavor -> flavor.setDishId(dishDto.getId()));
        dishFlavorService.saveBatch(dishDto.getFlavors());
    }

    @Transactional
    public void removeWithDish(Long[] ids) {
        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("category_id", Arrays.asList(ids));
        queryWrapper.eq("status", 1);
        int count = count(queryWrapper);
        if (count > 0) {
            throw new CustomerException("当前分类下关联了菜品，不能删除");
        }
        QueryWrapper<Dish> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.in("category_id", Arrays.asList(ids));
        queryWrapper1.eq("status", 1);
        int count1 = count(queryWrapper1);
        if (count1 > 0) {
            throw new CustomerException("当前分类下关联了套餐，不能删除");
        }
        this.removeByIds(Arrays.asList(ids));
    }

    @Override
    public List<DishDto> listWithFlavors(Dish dish) {
        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(dish.getCategoryId() != null, "category_id", dish.getCategoryId());
        queryWrapper.eq(StringUtils.isNotBlank(dish.getName()), "name", dish.getName());
        queryWrapper.eq("status", 1);
        queryWrapper.orderByAsc("sort").orderByAsc("update_time");
        List<Dish> list = list(queryWrapper);
        //将每个Dish对象转换为DishDto，放入dishDtoList集合中，响应
        ArrayList<DishDto> dishDtoList = new ArrayList<>();
        list.forEach(item ->
        {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item, dishDto);
            //根据菜品id查询对应的口味集合
            QueryWrapper<DishFlavor> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("dish_id", item.getId());
            List<DishFlavor> dishFlavorList = dishFlavorService.list(queryWrapper2);
            //设置菜品口味的数据
            dishDto.setFlavors(dishFlavorList);
            dishDtoList.add(dishDto);
        });
        return dishDtoList;
    }
}
